﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace Common
{
    public class MyHttpWebRequest
    {
        public static string Post(string strURL, string paramString)
        {
            string pagehtml = string.Empty;
            try
            {
                Encoding myEncoding = Encoding.GetEncoding("UTF-8");
                Uri myUri = new Uri(strURL);
                byte[] paramBytes = myEncoding.GetBytes(paramString);
                HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
                myWebRequest.ContentType = "application/json;charset=UTF-8";
                myWebRequest.Accept = "*/*";
                myWebRequest.AllowAutoRedirect = true;
                myWebRequest.Method = "post";
                myWebRequest.Timeout = 60000;
                myWebRequest.ContentLength = paramBytes.Length;
                //Send request
                using (Stream requestStream = myWebRequest.GetRequestStream())
                {
                    requestStream.Write(paramBytes, 0, paramBytes.Length);
                    requestStream.Close();
                }
                //Get response
                HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
                using (StreamReader myStreamReader = new StreamReader(myWebResponse.GetResponseStream(), myEncoding))
                {
                    pagehtml = myStreamReader.ReadToEnd();
                    myStreamReader.Close();
                }
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
            return pagehtml;
        }
    }
}
